package com.plant.serviceImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.plant.dao.BaseDao;
import com.plant.entity.PgcHottestFaq;
import com.plant.entity.PgcMonthList;
import com.plant.entity.PgcWeekList;
import com.plant.entity.UgcFaq;
import com.plant.entity.UgcFaqAnswer;
import com.plant.entity.UgcFriends;
import com.plant.entity.UgcUser;
import com.plant.service.FuzzySearchService;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
@Transactional
public class FuzzySearchServiceImpl implements FuzzySearchService{

    @Resource(name = "baseDaoImpl")//注解扫描basedao接口方法
    private BaseDao baseDao;

	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> FuzzySearchFriend(UgcUser u) {//----通过MYId查询用户
		String sql="SELECT id user_id,nickname,photo FROM ugc_user where my_id=?";
		String parm="ID:"+u.getMyId();
		return baseDao.SearchInfBysql(sql, null, parm);
	}

	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> FuzzySearchFriendByTel(UgcUser u) {//--通过用户手机号搜索好友
		String sql="SELECT id user_id,nickname,photo  FROM ugc_user where tel='"+u.getTel()+"'";
		return baseDao.Bysql(sql, null);
	}

	@SuppressWarnings("unchecked")
	public boolean Isfriend(UgcFriends u) {
		List<Object> list=new ArrayList<Object>();
		String sql="SELECT id FROM ugc_friends where apply_user_id="+u.getApplyUserId()+" and receive_user_id="+u.getReceiveUserId()+"";
		list=baseDao.Bysql(sql, UgcUser.class);
		List<Object> list1=new ArrayList<Object>();
		String sql1="SELECT id FROM ugc_friends where receive_user_id="+u.getApplyUserId()+" and apply_user_id="+u.getReceiveUserId()+"";
		list1=baseDao.Bysql(sql1, UgcUser.class);
		int size=list.size();
		int size1=list1.size();
		if(size>0||size1>0){
			return true;
		}else{
			return false;
		}
	}

	public List<Object[]> FuzzySearchPlants(String parm) {
		
		return null;
	}

	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> FuzzySeachArticle(Integer page,String parm) {//---模糊搜索文章
		String sql="SELECT id id_key,user_id,title,image,content,add_time create_time FROM pgc_article where title like '%"+parm+"%' or content like '%"+parm+"%' order by create_time desc limit "+(page-1)*3+",3";
		return baseDao.Bysql(sql, null);
	}
	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> MoreFuzzySeachArticle(String time,Integer page,String parm) {//---模糊搜索文章
		String sql="SELECT id id_key,user_id,title,image,content,add_time create_time FROM pgc_article where title like '%"+parm+"%' or content like '%"+parm+"%' and add_time<'"+time+"' order by create_time desc limit "+(page-1)*10+",10";
		return baseDao.Bysql(sql, null);
	}
	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> FuzzySeachActivity(Integer page,String parm) {//---模糊搜索活动
		String sql="SELECT id id_key,title,image,create_time,city,province,start_date,end_date FROM pgc_activity where title like '%"+parm+"%' order by create_time desc limit "+(page-1)*3+",3";
		return baseDao.Bysql(sql, null);
	}
	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> MoreFuzzySeachActivity(String time,Integer page,String parm) {//---模糊搜索活动
		String sql="SELECT id id_key,title,image,create_time,city,province,start_date,end_date FROM pgc_activity where title like '%"+parm+"%' and create_time<'"+time+"' order by create_time desc limit "+(page-1)*10+",10";
		return baseDao.Bysql(sql, null);
	}
	
	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> FuzzySeachTopic(String parm) {//---模糊搜索话题
		String sql="SELECT id id_key,user_id,title,image_url image,content FROM pgc_topic where title like '%"+parm+"%' or content like '%"+parm+"%' limit 0,3";
		return baseDao.Bysql(sql, null);
	}
	
	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> FuzzySeachFaq(String parm) {//---模糊搜索求助
		String sql="SELECT a.id id_key,a.user_id,a.title,b.nickname,b.photo,a.create_time FROM ugc_faq as a,ugc_user as b where a.user_id=b.id and a.title like '%"+parm+"%'";
		return baseDao.Bysql(sql, null);
	}
	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> FuzzySeachFaqPaging(String parm,Integer page) {//---模糊搜索求助
		String sql="select a.id id_key,COUNT(DISTINCT a.id),a.user_id,a.title,a.create_time,b.id content_id,b.content from ugc_faq a LEFT JOIN " +
				"ugc_faq_answer b on a.id=b.to_faq_id where a.title like '%"+parm+"%' or b.content like '%"+parm+"%' GROUP BY a.id limit "+(page-1)*10+",10";
		return baseDao.Bysql(sql, null);
	}

	@SuppressWarnings("unchecked")
	public List<Object> faqImage(Object obj) {
		String sql="SELECT url FROM ugc_upload_images where from_content_id="+obj+" and from_content_type=3 LIMIT 0,1";
		return baseDao.Bysql(sql, UgcFaq.class);
	}

	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> FuzzySearchFaqAnswer(String parm) {//模糊搜索答案
		String sql="SELECT a.id id_key,COUNT(DISTINCT a.id),a.title,b.content,c.id user_id,c.nickname,c.photo,a.create_time FROM ugc_faq as a,ugc_faq_answer as b,ugc_user as c WHERE a.id=b.to_faq_id AND a.user_id=c.id AND b.content LIKE '%"+parm+"%' GROUP BY a.id";
		return baseDao.Bysql(sql, null);
	}

	@SuppressWarnings("unchecked")
	public List<Object> OneFaqAnswer(Object obj) {
		String sql="select content from ugc_faq_answer where to_faq_id="+obj+" limit 0,1";
		return baseDao.Bysql(sql, UgcFaqAnswer.class);
	}

	public Long CountAllFaqAnswerCount(Object obj) {
		String hql="select count(id) from UgcLike where toContentId in ("+obj+") and toContentType=3";
		return baseDao.CountHql(hql);
	}

	@SuppressWarnings("unchecked")
	public List<Object> AllAnswerIds(Object obj) {
		String hql="select id from UgcFaqAnswer where toFaqId="+obj+"";
		return baseDao.find(hql);
	}

	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> ListAllFaq() {
		String sql="SELECT a.id id_key,a.user_id,a.title,b.nickname,b.photo FROM ugc_faq as a,ugc_user as b WHERE a.user_id=b.id";
		return baseDao.Bysql(sql, null);
	}
	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> ListAllFaqId() {
		String sql="SELECT id id_key,user_id,title,create_time FROM ugc_faq";
		return baseDao.Bysql(sql, null);
	}
	public boolean AddhotFaq(PgcHottestFaq hotfaq) {
		return baseDao.add(hotfaq);
	}

	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> HotFaq(Integer page,PgcHottestFaq hotfaq) {
		String sql="SELECT a.id id_key,a.user_id,a.faq_id,a.faq_title,a.sort,a.faq_answer_count,a.faq_answer_like count_like,a.faq_create_time,b.nickname,b.photo FROM pgc_hottest_faq as a,ugc_user as b WHERE a.user_id =b.id ORDER BY create_time DESC LIMIT "+(page-1)*10+",10";
		return baseDao.Bysql(sql, null);
	}

	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> HelpedPeople(String firstday,String lastday) {
		String sql="SELECT to_faq_id,COUNT(DISTINCT to_faq_id),user_id FROM ugc_faq_answer WHERE create_time<'"+lastday+"' AND create_time >'"+firstday+"' GROUP BY to_faq_id ORDER BY create_time DESC";
		return baseDao.Bysql(sql, null);
	}

	@SuppressWarnings("unchecked")
	public Integer HelpedCount(UgcUser u) {
		List<Object> list=new ArrayList<Object>();
		String sql="SELECT to_faq_id,COUNT(DISTINCT to_faq_id) FROM ugc_faq_answer WHERE user_id="+u.getId()+" GROUP BY to_faq_id";
		list=baseDao.Bysql(sql, UgcUser.class);
		int len=list.size();
		return len;
	}

	public Long OneUserAnswerBylikeCount(UgcUser u) {
		String hql="SELECT COUNT(id) FROM UgcLike WHERE toUserId="+u.getId()+" AND toContentType=3";
		return baseDao.CountHql(hql);
	}

	public boolean AddHotWeekList(PgcWeekList pw) {
		return baseDao.add(pw);
	}

	public boolean AddHotMonthList(PgcMonthList pm) {
		return baseDao.add(pm);
	}

	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> HotWeekPeople() {
		String sql="SELECT a.id id_key,a.user_id,a.faq_like_count,a.helped_people_count,b.nickname,b.photo " +
				"from pgc_week_list as a,ugc_user as b WHERE a.user_id=b.id ORDER BY create_time DESC LIMIT 0,3";
		return baseDao.Bysql(sql, null);
	}
    
	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> HotMonthPeople() {
		String sql="SELECT a.id id_key,a.user_id,a.faq_like_count,a.helped_people_count,b.nickname,b.photo " +
				"from pgc_month_list as a,ugc_user as b WHERE a.user_id=b.id ORDER BY create_time DESC LIMIT 0,3";
		return baseDao.Bysql(sql, null);
	}

	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> ActivePeople() {
		String sql="SELECT a.user_id,COUNT(DISTINCT a.user_id),b.nickname,b.photo FROM ugc_faq_answer as a,ugc_user " +
				"as b WHERE a.user_id=b.id GROUP BY a.user_id ORDER BY RAND() LIMIT 0,6";
		return baseDao.Bysql(sql, null);
	}
	
    
}
